package com.boyaa.shells.tool 
{
	import com.boyaa.fly.Timepiece;
	import com.boyaa.shells.control.ButtonManager;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	/**
	 * 滚动条
	 * @author fly
	 */
	public class OnScroll
	{
		
		private var _up:MovieClip = null;
		private var _down:MovieClip = null;
		private var _bar:MovieClip = null;
		private var _type:String = "";
		public var _top:int = 17;
		public var _length:int = 116;
		private var _callBack:Function = null;
		
		public function OnScroll(mc:MovieClip, fun:Function = null) 
		{
			_up = mc.up;
			_down = mc.down;
			_bar = mc.bar;
			_callBack = fun;
			_bar.scaleY = 1;
			//var rect:Rectangle = new Rectangle(4,4, _bar.width - 8, _bar.height - 8);
			_bar.scale9Grid = new Rectangle(4,4, _bar.width - 8, _bar.height - 8);
		}
		
		
		public function addEvent():void
		{
			_up.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_up.addEventListener(MouseEvent.MOUSE_UP, onUp);
			_down.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_down.addEventListener(MouseEvent.MOUSE_UP, onUp);
			_bar.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			
			_up.addEventListener(MouseEvent.ROLL_OUT, onUp);
			_down.addEventListener(MouseEvent.ROLL_OUT, onUp);	
			ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _up);
			ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _down);
			ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _bar);
		}
		
		public function removeEvent():void
		{
			_up.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_up.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			_down.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_down.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			_bar.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			
			_up.removeEventListener(MouseEvent.ROLL_OUT, onUp);
			_down.removeEventListener(MouseEvent.ROLL_OUT, onUp);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _up);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _down);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _bar);
		}
		
		
		public function setScale(num:Number):void
		{
			if (num >= 1)
			{
				_bar.visible = false;
				_bar.mouseEnabled = false;
				_up.mouseEnabled = false;
				_down.mouseEnabled = false;
			}
			else
			{
				_bar.visible = true;
				_bar.mouseEnabled = true;
				_up.mouseEnabled = true;
				_down.mouseEnabled = true;
				//_bar.y = _top;//功能为滚动条置顶，但当滚动条不在最上面的时候，也有需要调用此函数，但此时无必要使滚动条置顶，如确实需要应再调用其他API
				_bar.height = num * _length;
			}
		}
		
		private function stopDrag(e:MouseEvent):void 
		{
			_bar.stopDrag();
			Timepiece.instance.removeFun(update);
			_bar.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDrag);
		}
		
		private function onDrag(e:MouseEvent):void
		{
			_bar.stage.addEventListener(MouseEvent.MOUSE_UP, stopDrag);
			_bar.startDrag(false, new Rectangle(_bar.x, _top, 0, _length - _bar.height));
			Timepiece.instance.addFun(update);
		}
		
		private function onUp(e:MouseEvent):void 
		{
			Timepiece.instance.removeFun(onMove);
			
		}
		
		private function onDown(e:MouseEvent):void 
		{
			if (e.currentTarget == _up)
				_type = "up";
			else
				_type = "down";
			
			Timepiece.instance.addFun(onMove);
		}
		
		private function onMove():void
		{
			if (_bar.visible == false) 
				return;
			if (_type == "up")
				_bar.y --;
			else
				_bar.y ++;
			if (_bar.y < _top)
				_bar.y = _top;
			else if (_bar.y > _top + _length - _bar.height)
				_bar.y = _top + _length - _bar.height;
			update();
		}
		
		public function update():void
		{
			var scale:Number = (_bar.y - _top) / (_length - _bar.height);
			_callBack(new Fixed().toFixed(scale));
		}
		
		public function moveDown():void
		{
			_bar.y = _top + _length - _bar.height;
			update();
		}
		
	}
	
}